package com.linux.huhx.codetools.transutil;

import org.junit.Assert;
import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by huhx on 2017-06-14.
 */
public class CodeToolUtils {
    // 将首字母大写
    public static String transforFirstLetterUpper(String string) {
        String str = string.substring(0, 1);
        return string.replaceFirst(str, str.toUpperCase());
    }

    // 将首字母小写
    public static String transforFirstLetterLower(String string) {
        String str = string.substring(0, 1);
        return string.replaceFirst(str, str.toLowerCase());
    }

    // 首字母不变，其他字母转换成小写
    public static String transOtherLetterLower(String string) {
        String str = string.substring(1);
        return string.replaceFirst(str, str.toLowerCase());
    }

    // 将user_name转换成userName的写法
    public static String getWithHumpContent(String string) {
        string = transOtherLetterLower(string);
        String resultString = string;
        Pattern pattern = Pattern.compile("_(.)");
        Matcher matcher = pattern.matcher(string);
        String result = "";
        while (matcher.find()) {
            result = matcher.group(1);
            resultString = string.replace("_" + result, result.toUpperCase());
        }
        return resultString;
    }

    @Test
    public void getWithHumpContent_test() {
        Assert.assertEquals("userName", getWithHumpContent("user_name"));
        Assert.assertEquals("UserName", getWithHumpContent("USER_NAMe"));
    }

    @Test
    public void transOtherLetterLower_test() {
        Assert.assertEquals("Username", transOtherLetterLower("UserNAME"));
    }
}
